<?php
// 公共控制器
class CommonAction extends Action {
    public function getDepartment(){
        $data=$this->getAllDepartment();
        $tree=$this->createTreeStuctDpt($data);
        $json=$this->createSelectHtml($tree);
        $this->assign('json',$json);
    }
    /**
     * [getRole description]角色列表数据
     * @return [type] [description]
     */
    public function getRole(){
        $role=M('Role');
        $this->roleData=$role->select();
    }
     /**
     * [getNowEmploye description]获取当前用户的信息
     * @param  [type] $employe_id [description]
     * @return [type]             [description]
     */
    public function getNowEmploye($employe_id){
        $employe = M("Employe");
        $data = $employe->where("employe_id='".$employe_id."'")->select();
        return $data;
    }

    /**
     * [judgeState description]根据状态id返回对应提示
     * @param  [type] $state [description]
     * @return [type]        [description]
     */
    public function judgeState($state){
        $temp='';
        switch ($state) {
            case 0:
                $temp='已保存';
                break;
            case 1:
                $temp='正在审批中';
                break;
            case 2:
                $temp='审批未通过';
                break;
            case 3:
                $temp='审批通过';
                break;
            default:
                $temp='';
                break;
        }
        return $temp;
    }


    /**
     * [getOneEmployeName description]根据id获取
     * @param  [type] $employe_id [description]
     * @return [type]             [description]
     */
    public function getOneEmployeName($employe_id){
        $employe = M("Employe");
        $data = $employe->where("employe_id='".$employe_id."'")->getField('employe_name');
        return $data;
    }

    /**
     * [getOneField description]根据id获取对应字段名的值
     * @param  [type] $fieldName  [description]
     * @param  [type] $employe_id [description]
     * @return [type]             [description]
     */
    public function getOneField($fieldName,$employe_id)
    {
        $employe = M("Employe");
        $data = $employe->where("employe_id='$employe_id'")->getField($fieldName);
        return $data;
    }

    /**
     * [getDepartmentName description]根据id获取部门名称
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getDepartmentName($id){
        $department = M("Department");
        $data = $department->where("dpt_id='".$id."'")->getField('dpt_name');
        return $data;
    }

    /**
     * [getPositionName description]根据id获取职位名称
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getPositionName($id){
        $position = M('Position');
        $data = $position->where("position_id='".$id."'")->getField('position_name');
        return $data;
    }

    public function getDeparmentSelect(){
        $department = M('Department');
        $data = $department->select();
        return $data;
    }

    /**
     * 生成树形结构数组(部门)
     * @param  [array] $data [数据]
     * @param  [string] $pid  [父节点编号]
     * @return [type]       [description]
     */
    public function createTreeStuctDpt($data, $pid = 0)
    {
        $tempAry = array();

        foreach ($data as $value) 
        {
            if ($value['pid'] == $pid)
            {
                $value['child'] = $this->createTreeStuctDpt($data, $value['id']);
                if (empty($value['child']))
                {
                    $value['child'] = array();
                }
                $tempAry[] = $value;
            }
        }

        return $tempAry;
    }

    /**
     * 生成select标签元素
     * @param  [type] $tree  [description]
     * @param  string $space [description]
     * @return [type]        [description]
     */
    public function createSelectHtml($tree, $space = '')
    {
        $html = '';
        $split = '├ ';

        for ($i = 0; $i < count($tree); $i++)
        {
            if ($tree[$i][$this->pid] == '0') $split = '';
            if ($i == count($tree) - 1 && $tree[$i][$this->pid] != '0') $split = '└ ';

            $html .= $this->createOptionHtml($tree[$i],$space.$split);

            if (!empty($tree[$i]['child']))
            {
                $html .= $this->createSelectHtml($tree[$i]['child'], 
                    $space.'&nbsp;&nbsp;&nbsp;&nbsp;');
            }
        }
        return $html;
    }

    public function createOptionHtml($data, $split)
    {
        $employe = M('Employe')->select();
        return sprintf('<option value="%s" %s %s>%s</option>', 
            $data['id'], 
            $data['id'] == $employe['employe_department_id'] ? 'selected="selected"' : '',
            $data['id'] == I('id') ? 'disabled="disabled"' : '',
            $split.$data['name']
        );
    }

	/**
     * 生成树形结构数组
     * @param  [array] $data [数据]
     * @param  [string] $pid  [父节点编号]
     * @return [type]       [description]
     */
    public function createTreeStuct($data, $pid = 0)
    {
        $tempAry = array();

        foreach ($data as $value) 
        {
            if ($value['pid'] == $pid)
            {
                $value['child'] = $this->createTreeStuct($data, $value['id']);
				if (empty($value['child']))
				{
                    $value['child'] = $this->getOneDepartment($value['id']);
				}
                $tempAry[] = $value;
            }
        }

        return $tempAry;
    }

    /**
     * [getOneDempartment description]获取一个部门的人员
     * @param  [type] $id [description]部门id
     * @return [type]     [description]
     */
    public function getOneDepartment($id){
        $tempAry = array();
        $employe = M('Employe');
        $data = $employe->where("employe_department_id = '".$id."'")->select();
        foreach ($data as $value) {
            $temp['id'] = $value['employe_id'];
            $temp['name'] = $value['employe_name'];
            $temp['child'] = array();
            $tempAry[] = $temp;
        }
        return $tempAry;
    }

    /**
     * [getAllDepartment description]获取部门关系表
     * @return [type] [description]
     */
    public function getAllDepartment(){
        $tempAry = array();
        $department = M('Department');
        $data = $department->select();
        foreach ($data as $value) {
            $temp['id'] = $value['dpt_id'];
            $temp['name'] = $value['dpt_name'];
            $temp['pid'] = $value['dpt_parentid'];
            $tempAry[] = $temp;
        }
        return $tempAry;
    }

    /**
     * [getAllEmploye description]获取各个部门下的所以员工数组
     * @return [type] [description]
     */
    public function getAllEmploye(){
        $tempAry = array();
        $lastId=0;
        $department = M('Department');
        $departmentData = $department->select();
        foreach ($departmentData as $value) {
            $temp['id'] = $value['dpt_id'];
            $temp['name'] = $value['dpt_name'];
            $temp['pid'] = $value['dpt_parentid'];
            $tempAry[] = $temp;
            $lastId=$value['dpt_id'];
        }
        $employe=M('Employe');
        $employeData=$employe->select();
        foreach ($employeData as $value) {
            $lastId++;
            $temp['id'] = $lastId;
            $temp['name'] = $value['employe_name'].':'.$value['employe_phone'];
            $temp['pid'] = $value['employe_department_id'];
            $tempAry[] = $temp;
        }
        return $tempAry;
    }

    /**
     * [createJsonString description]生成规格的json格式
     * @param  [type] $tree [description]
     * @return [type]       [description]
     */
    public function createJsonString($tree)
    {
        $jsonstr = '';

        //sprintf('{" id":"%s","text": "%s","iconCls":"icon-mem-gear",%s"children":[',
        
        foreach ($tree as $value)
        {
            $jsonstr.= sprintf('{"id":"%s","text": "%s","iconCls":"icon-notice",%s"children":[',
                $value['id'], $value['name'], ''
            );

            if (!empty($value['child']))
            {
                $jsonstr .= $this->createJsonString($value['child']).']},';
            }
            else
            {
                $jsonstr .=']},';
            }
            
            //$html = substr($html,0,-1);
        }
        $jsonstr = substr($jsonstr,0,-1);
        return $jsonstr;
    }


    /**
     * [havePermission description]验证权限
     * @return [type] [description]
     */
    public function havePermission()
    {
        $employe=M('Employe');
        $employe_id=session('employe_id');
        $employe_role=$employe->where("employe_id='".$employe_id."'")->getField("employe_role");

        $model=new Model();
        $list=$model->table('oa_role_permission orp,oa_permission op')->where("orp.pmn_id=op.pmn_id and orp.role_id='".$employe_role."'")->select();

        $nlist=array();
        foreach ($list as $value) {
            $nlist[]=$value['pmn_name'];
        }
        return $nlist;
    }

     /*
     * 暴力锁定
     * [
     *   ip => [
     *      state => 'unlock',
     *      count => 1,
     *      time => time()
     *   ]
     * ]
     */
    public function Lock() {
        $ip = get_client_ip();
        $obj = S($ip);

        if (empty($obj)) {
            $obj = array(
                $ip => array(
                    'state' => 'unlock',
                    'count' => 1,
                    'time' => time()
                ) 
            );
            S($ip, $obj);
            return true;
        }
        else {
            if ($obj[$ip]['state'] == 'lock') {
                if (time() - $obj[$ip]['time'] >= 10 * 60) {
                    S($ip, null);
                    return true;
                }
                return false;
            }
            else {
                $obj[$ip]['count']++;
                if ($obj[$ip]['count'] == 6) {
                    $obj[$ip]['state'] = 'lock';
                    $obj[$ip]['time'] = time();
                    S($ip, $obj);
                    return false;
                }
                S($ip, $obj);
                return true;
            }
        }
    }

    /**
     * 解除锁定
     */
    public function UnLock() {
        $ip = get_client_ip();
        S($ip, null);
    }

}
